package com.platform.controller;

import com.platform.dto.OrderExcelDTO;
import com.platform.dto.OrderGoodsReturnDTO;
import com.platform.entity.OrderEntity;
import com.platform.entity.ReturnEntity;
import com.platform.service.OrderGoodsService;
import com.platform.service.OrderService;
import com.platform.service.ReturnService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.excel.ExcelExport;
import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.util.*;


/**
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-13 10:41:09
 */
@RestController
@RequestMapping("return")
public class ReturnController {
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                try {
                    setValue(DateUtil.parseDate(text));
                } catch (DateParseException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderGoodsService orderGoodsService;

    @Autowired
    private ReturnService returnService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("return:list")
    public R list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);

        List<ReturnEntity> orderList = returnService.queryList(query);
        int total = returnService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(orderList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("return:info")
    public R info(@PathVariable("id") Long id) {
        OrderGoodsReturnDTO orderGoodsReturnDTO = returnService.queryDetail(id);
        return R.ok().put("returnGoods", orderGoodsReturnDTO);
    }

    @RequestMapping("/saveAudit")
    @RequiresPermissions("return:audit")
    public R saveAudit(@RequestBody OrderGoodsReturnDTO orderGoodsReturnDTO){
        return returnService.saveAudit(orderGoodsReturnDTO);
    }

    @RequestMapping("/closeReturn")
    @RequiresPermissions("return:close")
    public R closeReturn(@RequestBody ReturnEntity returnEntity){
        return returnService.closeReturn(returnEntity);
    }

}
